package com.example.tivi.ui;

import io.vov.utils.Log;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.tivi.Config;
import com.example.tivi.R;
import com.example.tivi.entity.Channel;
import com.example.tivi.entity.GlobalInfor;
import com.example.tivi.imageloader.ImageLoader;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class ChannelFragment extends Fragment {
	private static final String TAG = "ChannelFragment";

	private ListView mlistChannel;
	private OnChannelClickListener mOnChannelClickListener;
	private ChannelAdapter mChannelAdapter;
	private ImageView mimgActionbar;
	private AdView mAdView;
	private TextView mtxtActionbarTitle;

	private OnActionbarClick mOnActionbarClick;

	private int mCategoryID;
	private String mTitle;

	private static final String EXTRA_CATEGORY_ID = "EXTRA_CATEGORY_ID";
	private static final String EXTRA_CATEGORY_NAME= "EXTRA_CATEGORY_NAME";
	

	public interface OnChannelClickListener {
		public void onChannelClick(Channel channel, List<Channel> listChannel);
	}

	public interface OnActionbarClick {
		public void onActionbarClick();
	}

	public static ChannelFragment newInstance(int categoryId,String categoryName) {
		Bundle bundle = new Bundle();
		bundle.putInt(EXTRA_CATEGORY_ID, categoryId);
		bundle.putString(EXTRA_CATEGORY_NAME,categoryName);
		ChannelFragment channelFragment = new ChannelFragment();
		channelFragment.setArguments(bundle);
		return channelFragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mOnChannelClickListener = (OnChannelClickListener) activity;
		mOnActionbarClick = (OnActionbarClick) activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mCategoryID = getArguments().getInt(EXTRA_CATEGORY_ID);
		mTitle=getArguments().getString(EXTRA_CATEGORY_NAME);
		Log.i(TAG, "title category="+mTitle);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_channel, container,
				false);
		initVieẉ(view);
		initAdView(view);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mChannelAdapter = new ChannelAdapter(getActivity(),
				GlobalInfor.getInstance().getChannelList(mCategoryID));
		mlistChannel.setAdapter(mChannelAdapter);
	}

	private void initVieẉ(View view) {
		mlistChannel = (ListView) view
				.findViewById(R.id.fragment_channel_list_channel);
		mimgActionbar = (ImageView) view
				.findViewById(R.id.header_img_actionbar);
		mtxtActionbarTitle=(TextView)view.findViewById(R.id.actionbarTitle);
		mtxtActionbarTitle.setText(mTitle);

		mimgActionbar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mOnActionbarClick.onActionbarClick();
			}
		});

		mlistChannel.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Channel channel = (Channel) ((ChannelAdapter) arg0.getAdapter())
						.getItem(arg2);
				mOnChannelClickListener.onChannelClick(channel,
						GlobalInfor.getInstance().getChannelList(mCategoryID));
			}
		});
	}

	private void initAdView(View view){
		mAdView=new AdView(getActivity(), AdSize.BANNER, Config.ADMOB);
		LinearLayout linearLayout=(LinearLayout) view.findViewById(R.id.fragment_channel_layout_adview);
		linearLayout.addView(mAdView);
		AdRequest adRequest=new AdRequest();
		//TODO Xoa bo test device
		adRequest.addTestDevice("C27236F177886F072102F9A4DD86EB78");
		mAdView.loadAd(adRequest);
	}
	

	private void loadData() {
		List<Channel> channels = GlobalInfor.getInstance().getChannelList(mCategoryID);
		mChannelAdapter.setChannelList(channels);
		mChannelAdapter.notifyDataSetChanged();
	}

	public void loadData(int categoryId,String categoryName) {
		mTitle=categoryName;
		mtxtActionbarTitle.setText(mTitle);
		mCategoryID = categoryId;
		loadData();
	}

	private class ChannelAdapter extends BaseAdapter {
		private final String TAG = "CategoryAdapter";
		private List<Channel> channels = new ArrayList<Channel>();
		private Context mContext;
		private ImageLoader mImageLoader;

		public ChannelAdapter(Context context, List<Channel> list) {
			mContext = context;
			mImageLoader=new ImageLoader(mContext);
			channels.addAll(list);
		}

		public void setChannelList(List<Channel> list) {
			channels.clear();
			channels.addAll(list);
		}

		@Override
		public int getCount() {
			return channels.size();
		}

		@Override
		public Object getItem(int position) {
			return channels.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = View.inflate(mContext,
						R.layout.item_list_channel, null);
				holder.imgThumnail = (ImageView) convertView
						.findViewById(R.id.item_list_category_img);
				holder.txtName = (TextView) convertView
						.findViewById(R.id.item_list_category_txt_title);
				holder.txtDes = (TextView) convertView
						.findViewById(R.id.item_list_category_txt_des);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.txtName.setText(channels.get(position).getName());
			holder.txtDes.setText(channels.get(position).getDescription());
//			mMyImageFetcher.mImageFetcher.loadImage(channels.get(position).getUrlThumnail(),
//					holder.imgThumnail);
			mImageLoader.DisplayImage(channels.get(position).getUrlThumnail(), holder.imgThumnail);
			Log.i(TAG, "Thumbnail=" + channels.get(position).getUrlThumnail());
			return convertView;
		}

		private class ViewHolder {
			ImageView imgThumnail;
			TextView txtName;
			TextView txtDes;
		}
	}
}
